package message;

import java.util.Date;
import java.util.Vector;

public class Switchboard implements Runnable {
	static final int MAX_MSG = 5;
	private Vector<String> vecMsg = new Vector<String>();
	
	@Override
	public void run() {
		while(true) {
			
			
			try {
				
				createMsg();
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	//tao message
	public synchronized void  createMsg() throws InterruptedException {
		if(vecMsg.size() == MAX_MSG) {
			wait();
			System.out.println("Current vector size: " + vecMsg.size());
		}
		String msg = new Date().toString();
		vecMsg.add(msg);
	}

	public synchronized String getMsg() throws InterruptedException {
		notify();
		
		if(vecMsg.size() == 0) {
			wait();
		}
		String msg = vecMsg.firstElement();
		vecMsg.remove(msg);
		
		return msg;
	}
	

}
